package com.ds.robot.assistant.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.collect.Lists;
import com.ds.robot.assistant.dao.ReqMilestonesDao;
import com.ds.robot.assistant.entity.ReqMilestonesEntity;
import com.ds.robot.assistant.model.MilestoneData;
import com.ds.robot.assistant.service.ReqMilestonesService;
import com.ds.robot.assistant.vo.ReqMilestoneVO;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.text.SimpleDateFormat;
import java.time.Instant;
import java.time.LocalDate;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;


@Service("reqMilestonesService")
public class ReqMilestonesServiceImpl extends ServiceImpl<ReqMilestonesDao, ReqMilestonesEntity> implements ReqMilestonesService {

    private SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");

    @Override
    public boolean checkDataExist(MilestoneData milestoneData) {
        List<ReqMilestonesEntity> list = this.lambdaQuery().eq(ReqMilestonesEntity::getReqName, milestoneData.getReqName()).list();
        return !CollectionUtils.isEmpty(list);
    }

    @Override
    public List<ReqMilestonesEntity> queryTodayMilestone() {
        Instant instant = LocalDate.now().atStartOfDay(ZoneId.systemDefault()).toInstant();
        // 只查询今天的
        return this.lambdaQuery().eq(ReqMilestonesEntity::getMilestoneDate, Date.from(instant))
                .orderByDesc(ReqMilestonesEntity::getMilestoneType)
                .orderByAsc(ReqMilestonesEntity::getTimePoint)
                .orderByAsc(ReqMilestonesEntity::getId)
                .list();
    }

    @Override
    public List<ReqMilestonesEntity> queryMilestoneByDate(String dateStr) {
        LocalDate localDate;
        try {
            localDate = LocalDate.parse(dateStr, DateTimeFormatter.ISO_LOCAL_DATE);
        } catch (Exception e) {
            log.error("日期格式错误", e);
            localDate = LocalDate.now();
        }
        Date date = Date.from(localDate.atStartOfDay(ZoneId.systemDefault()).toInstant());

        return this.lambdaQuery().eq(ReqMilestonesEntity::getMilestoneDate, date)
                .orderByDesc(ReqMilestonesEntity::getMilestoneType)
                .orderByAsc(ReqMilestonesEntity::getTimePoint)
                .orderByAsc(ReqMilestonesEntity::getId)
                .list();
    }


    @Override
    public void updateMilestone(ReqMilestoneVO vo) {
        this.lambdaUpdate()
                .set(ReqMilestonesEntity::getMilestoneType, vo.getMilestoneType())
                .set(ReqMilestonesEntity::getTimePoint, vo.getTimePoint())
                .set(ReqMilestonesEntity::getReminderUser, vo.getReminderUser())
                .set(ReqMilestonesEntity::getNotes, vo.getNotes())
                .eq(ReqMilestonesEntity::getReqName, vo.getReqName())
                .update();
    }

    @Override
    public void deleteByReqName(String reqName) {
        // 定义一个字符数组，包含所有需要转义的特殊字符
        // char[] specialChars = {'+', '%', '&', '#'};
        //
        // // 定义一个对应的转义字符或字符串
        // String escapeChar = "\\";
        //
        // // 对每个特殊字符进行转义
        // for (char c : specialChars) {
        //     reqName = reqName.replace(String.valueOf(c), escapeChar + c);
        // }
        //
        // // 定义一个正则表达式，匹配所有需要转义的特殊字符
        // String regex = "[+&%#]"; // 这个正则表达式匹配任意一个位于方括号内的字符
        //
        // // 使用 Matcher 和 Pattern 来进行替换
        // Pattern pattern = Pattern.compile(regex);
        // Matcher matcher = pattern.matcher(reqName);
        // String safeReqName = matcher.replaceAll(escapeChar + "$0");
        //
        // // 现在可以安全地使用这个经过转义处理的 safeReqName
        // this.lambdaUpdate().eq(ReqMilestonesEntity::getReqName, safeReqName).remove();


        // 现在可以安全地使用这个经过转义处理的 reqName
        this.lambdaUpdate().eq(ReqMilestonesEntity::getReqName, reqName).remove();
    }

    @Override
    public List<MilestoneData> queryAllData() {
        List<ReqMilestonesEntity> list = this.list();

        if (CollectionUtils.isEmpty(list)) {
            return Lists.newArrayList();
        }
        return list.stream().map(entity -> {
            MilestoneData data = new MilestoneData();
            BeanUtils.copyProperties(entity, data);
            return data;
        }).collect(Collectors.toList());
    }


    public static void main(String[] args) {
        String escapeChar = "\\KKK";
        String reqName = "用户分层优化+羊毛党&通知";

        // 定义一个正则表达式，匹配所有需要转义的特殊字符
        String regex = "[+&%#]"; // 这个正则表达式匹配任意一个位于方括号内的字符

        // 使用 Matcher 和 Pattern 来进行替换
        Pattern pattern = Pattern.compile(regex);
        Matcher matcher = pattern.matcher(reqName);

        // 使用 StringBuilder 构建新的字符串
        StringBuffer sb = new StringBuffer();

        while (matcher.find()) {
            // System.out.println("matcher = " + matcher.group());
            // 在特殊字符前加上反斜杠
            matcher.appendReplacement(sb, escapeChar + matcher.group());
        }
        // matcher.appendTail(sb);

        String safeReqName = sb.toString();
        System.out.println("safeReqName = " + safeReqName);
    }
}
